//
//  RACTargetQueueScheduler.m
//  ReactiveCocoa
//
//  Created by Josh Abernathy on 6/6/13.
//  Copyright (c) 2013 GitHub, Inc. All rights reserved.
//

#import "RACTargetQueueScheduler.h"
#import "RACQueueScheduler+Subclass.h"

@implementation RACTargetQueueScheduler

#pragma mark Lifecycle

- (id)initWithName:(NSString *)name targetQueue:(dispatch_queue_t)targetQueue {
	NSCParameterAssert(targetQueue != NULL);

	if (name == nil) {
		name = [NSString stringWithFormat:@"com.ReactiveCocoa.RACTargetQueueScheduler(%s)", dispatch_queue_get_label(targetQueue)];
	}

	dispatch_queue_t queue = dispatch_queue_create(name.UTF8String, DISPATCH_QUEUE_SERIAL);
	if (queue == NULL) return nil;

    /*
     dispatch_set_target_queue方法主要有两个目的：
     
     1. 设置dispatch_queue_create创建队列的优先级，
     dispatch_queue_create创建队列，不管是串行还是并行，它们的优先级都是DISPATCH_QUEUE_PRIORITY_DEFAULT级别
     
     dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
     dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
     dispatch_set_target_queue(serialQueue, globalQueue);
     可将serailQueue设置成DISPATCH_QUEUE_PRIORITY_HIGH。
     
     2. 建立队列的执行阶层。
     dispatch_set_target_queue(queue, targetQueue);
     这样设置时，相当于将queue指派给targetQueue，如果targetQueue是串行队列，则queue是串行执行的；如果targetQueue是并行队列，那么queue是并行的
     
     
     */
	dispatch_set_target_queue(queue, targetQueue);

	return [super initWithName:name queue:queue];
}

@end
